var table;
var $;
var layer;
var form;

var allNum=0;
var exmamineTrue=0;
var waitExamine=0;
var exmamineFalse=0;
var count=0;
layui.use(['table','jquery','layer','form'],function () {
    var table = layui.table;
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;

    $("#b1").removeClass("layui-btn-primary");
    $("#b1").addClass("layui-bg-cyan");
    $("#b1").click(function () {
        $(".buttons").addClass("layui-btn-primary");
        $(".buttons").removeClass("layui-bg-cyan");
        $("#b1").addClass("layui-bg-cyan");
    })
    $("#b2").click(function () {
        $(".buttons").addClass("layui-btn-primary");
        $(".buttons").removeClass("layui-bg-cyan");
        $("#b2").addClass("layui-bg-cyan");
    })
    $("#b3").click(function () {
        $(".buttons").addClass("layui-btn-primary");
        $(".buttons").removeClass("layui-bg-cyan");
        $("#b3").addClass("layui-bg-cyan");
    })
    $("#b4").click(function () {
        $(".buttons").addClass("layui-btn-primary");
        $(".buttons").removeClass("layui-bg-cyan");
        $("#b4").addClass("layui-bg-cyan");
    })

    /**获取数字*/
    getNum();
    function getNum() {
        $.ajax({
            url: '/order/getAllCourseOrder',
            type: 'post',
            dataType: 'json',
            data: {'type':2 },
            success: function (data) {
                allNum=data.data.length;
                for(var i = 0 ; i < data.data.length; i++) {
                    if(data.data[i].orderStatus==2) {
                        exmamineTrue++;
                    }
                    if(data.data[i].comment==2){
                        waitExamine++;
                    }
                    if(data.data[i].orderStatus==6){
                        exmamineFalse++;
                    }
                }
            }
        });
    }

    /*初始化表对象*/
    var orderTable = table.render({
        elem:"#root",
        url:'/order/getAllCourseOrder?type='+2,
        toolbar:'#toptool',
        defaultToolbar:['print','filter','exports'],
        page:true,
        cols:[[
            {type:'checkbox',align:'center',width:60},
            {type:'orderId',title:'序号',align:'center',width:60,templet:function (data) {
                    return data.LAY_INDEX;
                }},
            {field:'nickname',title:'用户昵称',align:'center'},
            {field:'phone',title:'用户手机',align:'center'},
            {field:'orderSource',title:'订单来源',align:'center',templet:function (data) {
                    if (data.orderSource=="2"){
                        data.orderSource = "小程序";
                    }else if (data.orderSource=="3"){
                        data.orderSource = "app";
                    }
                    return "<p>"+data.orderSource+"</p>";
                }},
            {field:'orderStatus',title:'订单状态',align:'center',templet:function (data) {
                    if (data.orderStatus=="2"){
                        data.orderStatus = "未付款";
                    }else if (data.orderStatus=="3"){
                        data.orderStatus = "已付款";
                    }else if(data.orderStatus=="4"){
                        data.orderStatus = "待发货";
                    }else if(data.orderStatus=="5"){
                        data.orderStatus = "已发货";
                    }else if(data.orderStatus=="6"){
                        data.orderStatus = "已签收";
                    }else if(data.orderStatus=="7"){
                        data.orderStatus = "退货申请";
                    }else if(data.orderStatus=="8"){
                        data.orderStatus = "退货中";
                    }else if(data.orderStatus=="9"){
                        data.orderStatus = "已退货";
                    }else if(data.orderStatus=="10"){
                        data.orderStatus = "取消交易";
                    }else if(data.orderStatus=="11"){
                        data.orderStatus = "拒绝退货";
                    }
                    return "<p>"+data.orderStatus+"</p>";
                }},
            {field:'courseName',title:'课程名称',align:'center'},
            {field:'courseIntro',title:'课程描述',align:'center'},
            {field:'orderPaymentChannel',title:'支付渠道',align:'center',templet:function(data){
                    if (data.orderPaymentChannel == "2"){
                        data.orderPaymentChannel = "微信支付";
                    }else if (data.orderPaymentChannel =="3"){
                        data.orderPaymentChannel = "余额支付";
                    }
                    return "<p>"+data.orderPaymentChannel+"</p>";
                }},
            {field:'trueName',title:'主讲师',align:'center'},
            {title:'操作',align:'center',templet:function (data) {
                    var tem = '<a  href="javascript:getLogById('+data.orderId+');" style="color:rgb(13,171,158)">查看</a>' +
                        '<a  href="javascript:removeLogById('+data.orderId+');" style="margin-left: 20px;color:rgb(13,171,158)">删除</a>';
                    return tem;
                }},
        ]]
    });

    /*初始化未评论，已完成表对象*/
    var orderTable2 = table.render({
        elem:"#root",
        url:'/order/getAllCourseOrder?type='+2,
        toolbar:'#toptool',
        defaultToolbar:['print','filter','exports'],
        page:true,
        cols:[[
            {type:'checkbox',align:'center',width:60},
            {type:'orderId',title:'序号',align:'center',width:60,templet:function (data) {
                    return data.LAY_INDEX;
                }},
            {field:'nickname',title:'用户昵称',align:'center'},
            {field:'phone',title:'用户手机',align:'center'},
            {field:'orderSource',title:'订单来源',align:'center',templet:function (data) {
                    if (data.orderSource=="2"){
                        data.orderSource = "小程序";
                    }else if (data.orderSource=="3"){
                        data.orderSource = "app";
                    }
                    return "<p>"+data.orderSource+"</p>";
                }},
            {field:'orderStatus',title:'订单状态',align:'center',templet:function (data) {
                    if (data.orderStatus=="2"){
                        data.orderStatus = "未付款";
                    }else if (data.orderStatus=="3"){
                        data.orderStatus = "已付款";
                    }else if(data.orderStatus=="4"){
                        data.orderStatus = "待发货";
                    }else if(data.orderStatus=="5"){
                        data.orderStatus = "已发货";
                    }else if(data.orderStatus=="6"){
                        data.orderStatus = "已签收";
                    }else if(data.orderStatus=="7"){
                        data.orderStatus = "退货申请";
                    }else if(data.orderStatus=="8"){
                        data.orderStatus = "退货中";
                    }else if(data.orderStatus=="9"){
                        data.orderStatus = "已退货";
                    }else if(data.orderStatus=="10"){
                        data.orderStatus = "取消交易";
                    }else if(data.orderStatus=="11"){
                        data.orderStatus = "拒绝退货";
                    }
                    return "<p>"+data.orderStatus+"</p>";
                }},
            {field:'courseName',title:'课程名称',align:'center'},
            {field:'courseIntro',title:'课程描述',align:'center'},
            {field:'orderPaymentChannel',title:'支付渠道',align:'center',templet:function(data){
                    if (data.orderPaymentChannel == "2"){
                        data.orderPaymentChannel = "微信支付";
                    }else if (data.orderPaymentChannel =="3"){
                        data.orderPaymentChannel = "余额支付";
                    }
                    return "<p>"+data.orderPaymentChannel+"</p>";
                }},
            {field:'trueName',title:'主讲师',align:'center'},
            {field:'comment',title:'是否评论',templet:function (data) {
                var str;
                if(data.comment==2){
                    str="待评论";
                }else if(data.comment==3){
                    str="已评论";
                }
                return "<p>"+str+"</p>";
            }},
            {title:'操作',align:'center',templet:function (data) {
                    var tem = '<a  href="javascript:getLogById('+data.orderId+');" style="color:rgb(13,171,158)">查看</a>' +
                        '<a  href="javascript:removeLogById('+data.orderId+');" style="margin-left: 20px;color:rgb(13,171,158)">删除</a>';
                    return tem;
                }},
        ]]
    });
    /*按钮切换*/
    //全部
    $("#b1").click(function(){
        all();
    })
    $("#b2").click(function(){
        $("#orderStats").html(" ");
        orderTable.reload({
            url:'/order/getAllCourseOrder?type='+2+'&orderStatus='+2
        });
    })
    $("#b3").click(function(){
        $("#orderStats").html(" ");
        orderTable2.reload({
            url:'/order/getAllCourseOrder?type='+2+'&comment='+2
        });
    })
    $("#b4").click(function(){
        $("#orderStats").html(" ");
        orderTable2.reload({
            url:'/order/getAllCourseOrder?type='+2+'&orderStatus='+6
        });
    })
    /*下拉框*/
    all();
    function all() {
        var str='<div class="layui-inline">'+
            '<label class="layui-form-label">订单状态</label>'+
            '<div class="layui-input-inline">'+
            '<select name="orderStatus" id="orderStatus">'+
            '<option value="">请选择</option>'+
            '<option value="2">未付款</option>'+
            '<option value="3">已付款</option>'+
            '<option value="4">待发货</option>'+
            '<option value="5">已发货</option>'+
            '<option value="6">已签收</option>'+
            '<option value="7">退货申请</option>'+
            '<option value="8">退货中</option>'+
            '<option value="9">已退货</option>'+
            '<option value="10">取消交易</option>'+
            '</select></div></div>'
        $("#orderStats").html(str);
        form.render('select');
        orderTable.reload({
            url:'/order/getAllCourseOrder?type='+2
        });
    }

    /* 删除 */
    removeLogById = function removeLogById(orderId){
        layer.confirm("确定删除？",{btn: ['确定', '取消'],title:"提示"}, function(){
            $.ajax({
                url:"/order/delCourseOrder",
                type:"post",
                dataType:"json",
                data:{"id":orderId},
                success:function(data){
                    layer.msg("删除成功");
                    orderTable.reload();
                }
            })
        });
    };

    /* 搜索 */
    form.on('submit(sub)',function (data) {
        orderTable.reload({
            url:'/order/getAllCourseOrder?orderPaymentChannel='+data.field.orderPaymentChannel+'&orderSource='+data.field.orderSource+'&orderStatus='+data.field.orderStatus+'$type='+2
        });
        return false;
    });
    /* 重置 */
    form.on('submit(subs)',function (data) {
        orderTable.reload({
            url:'/order/getAllCourseOrder'
        });
    });
    /* 刷新 */
    form.on('submit(Refresh)',function (data) {
        orderTable.reload({
            url:'/course/getAllCourse'
        });
    });
    /* 订单详情 */
    getLogById = function getLogById(orderId) {
        $.ajax({
            url: "/order/getUserMsg",
            type: "post",
            dataType: "json",
            data: {"orderId": orderId},
            success: function (data) {
                devicesMsg(data);
            },
        });
    }
    function devicesMsg(data) {
        layer.open({
            type: 2,
            title: "订单详情",
            area: ['800px', '600px'],
            maxmin:true,
            offset: 'auto',
            content: "/order/course-details.html",
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                obj.find("#account").val(data.data.account);
                obj.find("#nickname").val(data.data.nickname);
                obj.find("#phone").val(data.data.phone);
                obj.find("#idCard").val(data.data.idCard);
                obj.find("#email").val(data.data.email);
                obj.find("#fullName").val(data.data.fullName);
                obj.find("#courseName").val(data.data.courseName);
                obj.find("#courseIntro").val(data.data.courseIntro);
            }
        });
    }



    /*刷新*/
    Refresh = function Refresh() {
        location.reload();
    }

    /*返回*/
    goBack = function goBack() {
        parent.$('#beforeHref').val($('.layui-elem-quote').children('span').eq(0).text());
        parent.before();
        return false;
    }

    /*显示数字*/
    var saveNum=self.setInterval("giveNum()",1000);
    /*显示数字*/
    giveNum = function giveNum(){
        $("#b1").html("全部商品("+allNum+")");
        $("#b2").html("待付款("+exmamineTrue+")");
        $("#b3").html("未评论("+waitExamine+")");
        $("#b4").html("已签收("+exmamineFalse+")");
        clearInterval(saveNum);
    }
});
